package com.cityguardian.threads;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.cityguardian.cityguardianapp.MapActivity;
import com.cityguardian.cityguardianapp.R;
import com.cityguardian.model.Alerta;
import com.cityguardian.ws.ConnectionThread;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.os.AsyncTask;
import android.util.Log;

public class AsyncTaskMarkers extends AsyncTask<LatLngBounds, Double, List<Alerta>> {
	
	private MapActivity activity;
	
	public AsyncTaskMarkers(MapActivity activity){
		this.activity=activity;
	}
	
    @Override
    protected void onPreExecute() {
        //Log.d("Markers", "Cargando posiciones");
    }
	
	@Override
	protected List<Alerta> doInBackground(LatLngBounds... params) {
		
		List<Alerta> response=cargarMarkers(params[0]);
		return response;
	}

	@Override
	protected void onPostExecute(List<Alerta> result) {
		
		if ( result== null){
			Log.d("Markers", "No hay alertas cercanas");
		}else{
			//if (activity.listaAlerta==null) activity.listaAlerta=result;
			for(Alerta alerta:result){
				if (!activity.listaAlerta.contains(alerta)){
					Marker marker=this.activity.mapa.addMarker(new MarkerOptions()
			        .position(new LatLng(Double.parseDouble(alerta.getPosicionY()), (Double.parseDouble(alerta.getPosicionX()))))
			        .title(alerta.getIdAlerta())
			        .snippet("alerta")
			        .icon(BitmapDescriptorFactory
			            .fromResource(R.drawable.pin_red)));
					activity.listaAlerta.add(alerta);
					Log.d("Alerta", marker.getTitle());
				}
			}
		}    	
	}
	
	private List<Alerta> cargarMarkers(LatLngBounds bounds) {//DEVOLVER LIST<ALERTA>
		
		LatLng northeast=bounds.northeast;
		LatLng southwest=bounds.southwest;
		
		Double posXini = southwest.longitude;
		Double posXfin = northeast.longitude;
		Double posYini = southwest.latitude;
		Double posYfin = northeast.latitude;
		
		List<NameValuePair> param = new ArrayList<NameValuePair>();								
		param.add(new BasicNameValuePair("posXini", posXini.toString()));
		param.add(new BasicNameValuePair("posXfin", posXfin.toString()));
		param.add(new BasicNameValuePair("posYini", posYini.toString()));
		param.add(new BasicNameValuePair("posYfin", posYfin.toString()));
		
		String route="/ws/alerta/get_positions_nearby/";
		Log.e("log_tag",param.toString());
		
		ConnectionThread connection = new ConnectionThread(param, route);
		String responseBody = connection.getResponse();
		
		if (responseBody==null){
			Log.e("log_tag", "Error in webservice");
			return null;
		}

	    Gson gson = new Gson();
	    Log.e("log_tag", responseBody);
		List<Alerta> listaPosiciones = new ArrayList<Alerta>();
		listaPosiciones = gson.fromJson(responseBody,new TypeToken<List<Alerta>>(){}.getType());	
		
		return listaPosiciones;
	}

}
